<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH. 'classes/controller/My_Controller.php');
require_once(APPPATH. 'classes/model/Model_UserProfile.php');
require_once(APPPATH. 'classes/model/Model_UserRelation.php');
require_once(APPPATH. 'classes/model/Model_UserComments.php');
require_once(APPPATH. 'classes/model/Model_UserFeeds.php');

class Controller_Welcome extends My_Controller {
	public function action_index() {
		$this->action_home();
	}
	public function action_home() {
		$place = Arr::get($_POST, 'place');
		$text = Arr::get($_POST, 'text');
		$comments = array();
		$ufeeds = new Model_UserFeeds();
		if($place && $text) {
			$ufeeds->addFeed($this->uid, Model_UserFeeds::TYPE_WALL, Model_UserFeeds::ACCESS_FRIENDS,
					 $place, $text);
		}
		$friend_uids = array();
		foreach($this->friends as $friend) {
			$friend_uids[] = $friend['uid'];
		}
		$friend_uids[] = $this->uid;
		if($this->partner_uid) $friend_uids[] = $this->partner_uid;
		$ucm = new Model_UserComments();
		$feeds = $ufeeds->getStreamFeeds($friend_uids, Model_UserFeeds::ACCESS_FRIENDS);
		$ucm->getFeedComments($feeds);
		$um = new Model_UserProfile();
		$view = View::factory('welcome');
		$view->user = $this->profile_data;
		$view->partner = $um->getUserProfileData($this->partner_uid);
		if($view->user['pic_url'] == '') $view->user['pic_url'] = My_URL::app_url("profile_pics/dummy.png",false);
		if($view->partner['pic_url'] == '') $view->partner['pic_url'] = My_URL::app_url("profile_pics/dummy.png",false);
		$view->feeds = $feeds;
		$this->renderLoggedInUserView($view);
	}
	public function action_post() {
		if(!$this->request->is_ajax()) {
			throw new Kohana_HTTP_Exception_500();
		}
		$feedid = Arr::get($_POST, 'feedid');
		$comment = Arr::get($_POST, 'comment');
		$ucm = new Model_UserComments();
		$ucm->addComment($feedid, $this->uid, $comment);
		echo HTML::chars($comment);
	}
	public function action_page($page_id) {
		$ucm = new Model_UserComments();
		$text = Arr::get($_POST, 'text');
		if($text) {
			$ucm->addPostByPageId($this->uid, $page_id, $text);
		}
		$view = View::factory('page');
		$view->uid = $this->uid;
		$view->page_id = $page_id;
		$view->page_name = $ucm->getPageName($page_id);
		$view->comments = $ucm->getPostsByPage($page_id);
		$this->renderLoggedInUserView($view);
	}
}